// 地图API调用模块
const API_BASE_URL = 'http://localhost:8080/api/map';

// 生成静态地图URL
export const generateMapUrl = async (mapData) => {
    try {
        const response = await fetch(`${API_BASE_URL}/url`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify(mapData)
        });
        
        if (!response.ok) {
            throw new Error(`HTTP error! status: ${response.status}`);
        }
        
        const result = await response.json();
        return result;
    } catch (error) {
        console.error('生成地图URL失败:', error);
        throw error;
    }
};

// 获取静态地图图片
export const getMapImage = async (mapData) => {
    try {
        const response = await fetch(`${API_BASE_URL}/image`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify(mapData)
        });
        
        if (!response.ok) {
            throw new Error(`HTTP error! status: ${response.status}`);
        }
        
        const blob = await response.blob();
        return URL.createObjectURL(blob);
    } catch (error) {
        console.error('获取地图图片失败:', error);
        throw error;
    }
};

// 获取简单地图URL
export const getSimpleMapUrl = async (longitude, latitude, zoom = 10) => {
    try {
        const response = await fetch(`${API_BASE_URL}/simple?longitude=${longitude}&latitude=${latitude}&zoom=${zoom}`);
        
        if (!response.ok) {
            throw new Error(`HTTP error! status: ${response.status}`);
        }
        
        const result = await response.json();
        return result;
    } catch (error) {
        console.error('获取简单地图URL失败:', error);
        throw error;
    }
};

// 健康检查
export const checkHealth = async () => {
    try {
        const response = await fetch(`${API_BASE_URL}/health`);
        
        if (!response.ok) {
            throw new Error(`HTTP error! status: ${response.status}`);
        }
        
        const result = await response.json();
        return result;
    } catch (error) {
        console.error('健康检查失败:', error);
        throw error;
    }
}; 